package com.cym.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAdvice {
    @Pointcut("execution(* com.cym.mapper.*.*(..))")
    public void pt() {
    }

    //  前置通知
    @Before("pt()")
    public void before() {
        System.out.println("前置通知");

        System.out.println("调用时间："+System.currentTimeMillis());
        System.out.println("方法名："+this.getClass().getName());
    }

    //  后置通知
    @After("pt()")
    public void after() {
        System.out.println("后置通知");
        System.out.println("调用时间："+System.currentTimeMillis());
    }

    //  环绕通知
    //@Around("pt()")
    public void around(ProceedingJoinPoint pjg){
        System.out.println("环绕通知");
        //获取调用方法的类名
        System.out.println("调用类名："+pjg.getTarget().getClass().getName());
        //获取方法名
        System.out.println("方法名："+pjg.getSignature().getName());
        //获取参数
        Object[] args = pjg.getArgs();
        for (Object arg : args) {
            System.out.println("参数："+arg);
        }
        System.out.println("调用时间："+System.currentTimeMillis());
        try {
            pjg.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        //结束时间
        System.out.println("结束时间："+System.currentTimeMillis());
    }

    @AfterThrowing(value="pt()",throwing="e")
    public void afterReturning(JoinPoint point,Exception e){
        System.out.println("异常通知");
        System.out.println("异常信息："+e.getMessage());
        System.out.println("调用类名："+point.getTarget().getClass().getName());
        System.out.println("方法名："+point.getSignature().getName());

    }
}
